import React, { Component } from 'react'

export default class Count extends Component {
    state={count:0}

    add = () => {
        const { value } = this.selectNumber
        const {count} = this.state
        // *1 进行强制类型转换
        this.setState({count: count + value*1 })
    }
    minus = () => {
        const { value } = this.selectNumber
        const {count} = this.state
        this.setState({count: count - value*1 })
    }
    addOdd = () => {
        const { value } = this.selectNumber
        const {count} = this.state
        if(count % 2 != 0) this.setState({count: count + value*1 })
    }
    addAsync = () => {
        const { value } = this.selectNumber
        const {count} = this.state
        setTimeout(()=>{
            this.setState({count: count + value*1 })
        },500)
    }
    render() {
        return (
            <div>
                <h1>当前求和为：{this.state.count}</h1>
                <select ref={c => this.selectNumber = c}>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                </select>
                <button onClick={this.add}>+</button>
                <button onClick={this.minus}>-</button>
                <button onClick={this.addOdd}>当前求和为奇数在加</button>
                <button onClick={this.addAsync}>异步加</button>
            </div>
        )
    }
}
